package com.ylbh.modules.sign.service.impl;


import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.sign.dto.EntityRewardDTO;
import com.ylbh.common.pojo.sign.vo.RewardAndCountVO;
import com.ylbh.common.pojo.sign.vo.ShoppingSigninRewardVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.Ref;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.sign.entity.ShoppingSigninReward;
import com.ylbh.modules.sign.mapper.ShoppingSigninRewardMapper;
import com.ylbh.modules.sign.service.ShoppingSigninRewardService;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ShoppingSigninRewardServiceImpl implements ShoppingSigninRewardService {

    @Autowired
    private ShoppingSigninRewardMapper shoppingSigninRewardMapper;
    @Override
    public void addSign(EntityRewardDTO entitySignDTO) {
        BeanMap map = new BeanMap(entitySignDTO);
        IdWorker idW = new IdWorker();
        Date date = new Date();
        for (Object key: map.keySet()) {
            if (!key.equals("class")&& !key.equals("eight") && !key.equals("nine") && !key.equals("ten")) {
                ShoppingSigninReward reward = new ShoppingSigninReward();
                reward.setCreateTime(date);
                reward.setCreateUserId(ShiroUtils.getUserId().toString());
                reward.setUpdateTime(date);
                reward.setUpdateUserId(ShiroUtils.getUserId().toString());
                reward.setId(idW.nextId());
                reward.setRewardType(1);
                reward.setEnableState(1);
                reward.setRewardDay(Integer.parseInt(map.get(key).toString().split(",")[0]));
                reward.setRewardContent(Integer.parseInt(map.get(key).toString().split(",")[1]));
                shoppingSigninRewardMapper.insertSignReward(reward);
            }else if (!key.equals("class")){
                ShoppingSigninReward reward = new ShoppingSigninReward();
                reward.setCreateTime(date);
                reward.setCreateUserId(ShiroUtils.getUserId().toString());
                reward.setUpdateTime(date);
                reward.setUpdateUserId(ShiroUtils.getUserId().toString());
                reward.setId(idW.nextId());
                reward.setId(idW.nextId());
                reward.setRewardType(2);
                reward.setEnableState(1);
                reward.setRewardDay(Integer.parseInt(map.get(key).toString().split(",")[0]));
                reward.setRewardContent(Integer.parseInt(map.get(key).toString().split(",")[1]));
                shoppingSigninRewardMapper.insertSignReward(reward);
            }
        }
    }

    @Override
    public RewardAndCountVO getSigRewardList() {
        List<ShoppingSigninRewardVO> list =shoppingSigninRewardMapper.selectSigRewardList();
        int count = shoppingSigninRewardMapper.selectSignEnableStateCount();
        RewardAndCountVO vo = new RewardAndCountVO();
        vo.setList(list);
        vo.setCount(count);
        return vo;
    }

    @Override
    public void removeRewardEnableState() {
        List<ShoppingSigninRewardVO> list =shoppingSigninRewardMapper.selectSigRewardList();
        for (ShoppingSigninRewardVO vo : list) {
            shoppingSigninRewardMapper.updateRewardEnableState(vo.getId(),0);
        }
    }
}
